package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.domain.Permission;
import cn.wolfcode.crud.util.JsonResult;
import cn.wolfcode.crud.qo.PageResult;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.service.IPermissionService;
import cn.wolfcode.crud.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    //查询分页后的结果显示给页面上
    @RequestMapping("/list")
    @RequiredPermission("权限页面")
    public String list(Model model, @ModelAttribute("qo")QueryObject qo) {

        model.addAttribute("pageInfo",permissionService.query(qo));
        return  "permission/list";
    }

    @RequiredPermission("权限删除页面")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null) {
            permissionService.delete(id);
        }
        return "redirect:/permission/list.do";
    }


    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload() {
        try {
            permissionService.reload();
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"加载失败");
        }
    }

}
